package com.alongstudio.action.client.settings;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alongstudio.account.action.base.BaseAction;
import com.alongstudio.account.commons.exception.BusinessException;
import com.alongstudio.account.constants.IViewConstant;
import com.alongstudio.account.entity.ClientUser;
import com.alongstudio.account.entity.Project;
import com.alongstudio.account.service.IProjectService;

@SuppressWarnings("serial")
@Component("ProjectAction")
@Scope("prototype")
public class ProjectAction extends BaseAction {

	@Autowired
	@Qualifier("ProjectService")
	private IProjectService projectService;
	private Integer id;
	private String projectName;
	private String OPERATOR_RESULT;
	private List<Project> projectList;
	private Project project;
	private String className = ProjectAction.class.getName();

	@Override
	public String executeAction() throws BusinessException {
		return SUCCESS;
	}

	public String getAllProject() throws BusinessException {
		try {
			projectList = projectService.findAllProject(getCurrentClientUserId());
		}
		catch (BusinessException b) {
			throw b;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		return SUCCESS;
	}

	public String newProject() throws BusinessException {
		boolean isOk = false;
//		try {
//			if (projectName != null) {
//				Project project = new Project();
//				project.setProjectName(projectName);
//
//				ClientUser clientUser = new ClientUser();
//				clientUser.setId(getCurrentClientUserId());
//				project.setClientUser(clientUser);
//				project.setCreateTime(new Date());
//				int id = projectService.createProject(project);
//				isOk = id > 0;
//			}
//		}
//		catch (BusinessException b) {
//			throw b;
//		}
//		catch (Exception e) {
//			throw new BusinessException(className, e);
//		}
		OPERATOR_RESULT = isOk ? IViewConstant.SAVE_SUCCESS : IViewConstant.SAVE_FAILED;
		return SUCCESS;
	}

	public String updateProject() throws BusinessException {
		boolean isOk = false;
		try {
			if (project != null) {
				project.setUpdateTime(new Date());
				isOk = projectService.updateProject(project);
			}
		}
		catch (BusinessException b) {
			throw b;
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		OPERATOR_RESULT = isOk ? IViewConstant.SAVE_SUCCESS : IViewConstant.SAVE_FAILED;
		return SUCCESS;
	}

	public String deleteProject() throws BusinessException {
		boolean isOk = false;
//		try {
//			if (id != null) {
//				isOk = projectService.deleteProject(id);
//			}
//		}
//		catch (BusinessException b) {
//			throw b;
//		}
//		catch (Exception e) {
//			throw new BusinessException(className, e);
//		}
		OPERATOR_RESULT = isOk ? IViewConstant.SAVE_SUCCESS : IViewConstant.SAVE_FAILED;
		return SUCCESS;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public String getOPERATOR_RESULT() {
		return OPERATOR_RESULT;
	}

	public void setOPERATOR_RESULT(String oPERATOR_RESULT) {
		OPERATOR_RESULT = oPERATOR_RESULT;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public List<Project> getProjectList() {
		return projectList;
	}

	public void setProjectList(List<Project> projectList) {
		this.projectList = projectList;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

}
